import Model from "js-model"
import helper from "../../common/helper.js"
const app = getApp()
class HomeModel extends Model {
  parse(data) {
    let home = super.parse(data)
		// 轮播图格式化地址
		if (helper.isArray(home.plan.ad_img)) {
			home.plan.ad_img.forEach(v=>{
				if(v.image_url.length == 0) {
				  v.image_url.push('https://wq.senwellzy.cn/Public/Images/h5shop/home_banner.png')
				} else {
					v.image_url = app.globalData.BACK_URL + v.image_url
				}
			})
		}
		// 商家logo以及banner格式化
		if (helper.isArray(home.comp)) {
			home.comp.forEach(v=>{
				if(v.image_url.length != 0) {
				  v.comp_banner = app.globalData.BACK_URL + v.comp_banner
				  v.image_url = app.globalData.BACK_URL + v.image_url
					v.comp_code = 'a' + v.comp_id
				}
			})
		}
		// 对券详细数据格式化
		if (helper.isArray(home.coupon)) {
			home.coupon.forEach(v=>{
				v.amount = parseFloat(v.amount)
				v.rebate = parseFloat(v.rebate)
				v.price = parseFloat(v.price)
				v.image_url = app.globalData.BACK_URL + v.image_url
				
				// 对品牌数据添加coupon_len字段
				if (helper.isArray(home.comp)) {
					home.comp.forEach(c => {
						if (v['comp_ids'] == c['comp_id']) c['coupon_len']++
					})
				}
			})
		}
    return home
  }
  dispose(data, param) {
     return super.dispose(data, param)
  }
}
export default new HomeModel({
	plan: {
		ad_img: [{
			id: {
				type: Number,
				default: 1
			},
			plan_id: Number,
			sort: Number,
			title: String,
			type: String,
			image_url: String,
			min_link: String,
			out_link: String,
			param_key: String,
			param_value: String
		}]
	},
	comp: [{
		comp_id: Number,
		comp_name: String,
		comp_banner: String,
		comp_short: String,
		comp_sort: Number,
		image_url: String,
		show_near_shop: String,
		comp_code: String,
		coupon_len: {
			type: Number,
			default: 0
		}
	}],
	coupon: [{
		act_name: String,
		act_num: String,
		amount: String,
		category: String,
		comp_ids: String,
		coupon_type: String,
		enddate: {
			type: Date,
			format: 'YYYY-MM-DD' //此处使用manba日期工具
		},
		explain: String,
		id: Number,
		image_url: String,
		instruction: String,
		is_extend: String,
		plantype: String,
		price: String,
		rebate: String,
		sort: Number,
		startdate: {
			type: Date,
			format: 'YYYY-MM-DD' //此处使用manba日期工具
		},
		stock: Number,
		time_limit_type: String,
		time_show: String,
	}]
})
